package controller;

import gdrObject.*;

import java.awt.Point;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;

import map.Map;
import mission.Mission;


import command.Attack;
import command.Attack.Type;

import Player.Faction;
import Player.PG;
import Player.Race;
import concept.RegenerationEffect;
import concept.RegenerationEffect.vitalStat;

public class LocalManager implements Serializable{
	
	public static String[] getAll(){
		String[] saves = null;
		saves = saveDir.list();
		if(saves == null)
			return new String[0];
		for(int i = 0; i<saves.length; i++){
			String s = saves[i];
			saves[i] = s.substring(0,s.length()-4);
		}
		return saves;
		
	}
	@SuppressWarnings("resource")
	public static boolean load(String name){
		File f = new File(saveDir.getAbsolutePath()+"/"+name+".dat");
		if(f.exists()){
			try {
				ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
				SerializableObject load = (SerializableObject)in.readObject();
				current = load.current;
				map = load.map;
				mission = load.mission;
				playerLocation = load.playerLocation;
				if(current != null)
					return true;
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	
	@SuppressWarnings("resource")
	public static void save(){
		File f = new File(saveDir.getAbsolutePath()+"/"+current.getName()+".dat");
		if(!saveDir.exists())
			saveDir.mkdir();
		try {
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
			out.writeObject(new SerializableObject(current, map, mission, playerLocation));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static boolean delete(String name){
		File f = new File(saveDir.getAbsolutePath()+"/"+name+".dat");
		if(f.exists()){
			boolean result = f.delete();
			return result;
		}
		return false;
	}
	
	public static PG createNew(String name, Race r, Faction f, String box, int index, String history){
		PG newplayer = new PG(name, r, f);
		newplayer.setHistory(history);
		newplayer.setAvatarBox(box);
		newplayer.setAvatarNum(index);
        try {
        	Collection<Attack> bast = new ArrayList<Attack>();
        	bast.add(new Attack("Colpo", Type.MELEE, 1, 3, 90));
            newplayer.addGdrObject(new Weapon("Bastone", bast, null, 0, 1, 1, 0));
            Collection<Attack> pie = new ArrayList<Attack>();
            pie.add(new Attack("Lancio", Type.DISTANCE, 1, 4, 50));
            newplayer.addGdrObject(new Weapon("Pietre", pie, null, 0, 0, 1, 0));
            Collection<Attack> basta = new ArrayList<Attack>();
            basta.add(new Attack("Pioggia di meteore", Type.MAGIC, 5, 22, 60));
            basta.add(new Attack("Lampo di luce", Type.MAGIC, 3, 15, 70));
            basta.add(new Attack("Piccolo dardo di ghiaccio", Type.MAGIC, 1, 9, 75));
            newplayer.addGdrObject(new Weapon("Bastone dell'apocalisse", basta, null, 10, 0, 0, 0));
            newplayer.addGdrObject(new Armor("Cuoio", 3, null, 4));
            newplayer.addGdrObject(new Usable("Pozione Vite Piccola", "Earth_128x128.png", new RegenerationEffect(vitalStat.LIFE, 20)));
        } catch (Exception ex) {
        	ex.printStackTrace();
        }
        current = newplayer;
        return newplayer;
	}
	
	public static PG getCurrentPlayer(){
		return current;
	}
	
	public static void setCurrentPlayer(PG player){
		current = player;
	}
	
	public static Mission getCurrentMission(){
    	return mission;
    }
	
	public static void setCurrentMission(Mission mission){
		LocalManager.mission = mission;
    }
	
	public static Point getPlayerLocation(){
    	return playerLocation;
    }

	public static void setPlayerLocation(Point location){
    	playerLocation = location;
    }
	
	public static Map getMap(){
		return map;
	}
	
	public static void setMap(Map m){
		map = m;
	}
	
	private static PG current;
	private static Map map;
	private static Mission mission;
	private static Point playerLocation;
	private static File saveDir = new File("save");
	
	static class SerializableObject implements Serializable{

		public SerializableObject(PG current, Map map, Mission mission,Point playerLocation) {
			super();
			this.current = current;
			this.map = map;
			this.mission = mission;
			this.playerLocation = playerLocation;
		}
		
		public PG current;
		public Map map;
		public Mission mission;
		public Point playerLocation;
		private static final long serialVersionUID = -2080504057576404472L;
	}
}
